//
//  HYOUMinGiftView.m
//  BestGift
//
//  Created by 温晓宇 on 15/12/30.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "HYOUMinGiftView.h"
#import "HYOUBabyCollectionViewCell.h"
#import "HYOUGiftCollectionReusableView.h"
@interface HYOUMinGiftView ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)HYOUBaseTableView *myTableView;
@property(nonatomic, retain)HYOUBaseCollectionView *myCollectionView;

@property(nonatomic, retain) NSMutableArray *nameArray;
@property(nonatomic, retain) NSMutableArray *subArray;
@property(nonatomic, retain) NSMutableArray *allArray;
@property(nonatomic, retain) NSMutableArray *array;

@end

@implementation HYOUMinGiftView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    self.myTableView = [[HYOUBaseTableView alloc] initWithFrame:CGRectMake(0, 0,(CGFloat)90/375 *WIDTH, HEIGHT - 40) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self addSubview:self.myTableView];
    
    [self.myTableView registerClass:[HYOUBaseTableViewCell class] forCellReuseIdentifier:@"TVcell"];
    
    self.myTableView.bounces = NO;
    self.myTableView.showsVerticalScrollIndicator = NO;
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((CGFloat)70/375 *WIDTH, (CGFloat)90/667 *HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 5;
    
    self.myCollectionView = [[HYOUBaseCollectionView alloc]initWithFrame:CGRectMake((CGFloat)80/375 *WIDTH, 10, (WIDTH - (CGFloat)80/375 *WIDTH), HEIGHT - 40) collectionViewLayout:flowLayout];
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    [self addSubview:self.myCollectionView];
    self.myCollectionView.pagingEnabled = YES;
    self.myCollectionView.bounces = YES;
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH - (CGFloat)100/375 *WIDTH, (CGFloat)100/667 *HEIGHT);
    self.myCollectionView.tag = 100;
    
    [self.myCollectionView registerClass:[HYOUBabyCollectionViewCell class] forCellWithReuseIdentifier:@"CVcell"];
    [self.myCollectionView registerClass:[HYOUGiftCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                                                                                   
    self.nameArray = [NSMutableArray array];
    self.subArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    
    [AppTools finalAFNWithURLString:@"http://api.liwushuo.com/v2/item_categories/tree" parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = resuposeObject;
        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
        NSMutableArray *categories = [data objectForKey:@"categories"];
        for (NSDictionary *dic in categories) {
            HYOUBigGift *model = [[HYOUBigGift alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.nameArray addObject:model.name];
            [self.subArray addObject:model.subcategories];
            [self.allArray addObject:model];
        }
        [self.myCollectionView reloadData];
        [self.myTableView reloadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYOUBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TVcell"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = [self.nameArray objectAtIndex:indexPath.row];
    cell.textLabel.highlightedTextColor = RGBA(231, 53, 62, 1);
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.myCollectionView.contentOffset = CGPointMake(0, (self.myCollectionView.frame.size.height - 150)*indexPath.row);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag == 100) {
        NSInteger count = scrollView.contentOffset.y/(self.myCollectionView.frame.size.height - 150);
        NSIndexPath *index = [NSIndexPath indexPathForRow:count inSection:0];
        // tableview选中对应index的cell
        [self.myTableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionNone];
        // 选中的tableviewCell滚动到屏幕中间位置
        [self.myTableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.subArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    self.array = self.subArray[section];
    return self.array.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
#pragma makr -- 需修改
    HYOUBabyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVcell" forIndexPath:indexPath];
    self.array = self.subArray[indexPath.section];
    cell.model = self.array[indexPath.item];
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //表头
    HYOUGiftCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    head.name = [self.nameArray objectAtIndex:indexPath.section];
    return head;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUBabyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVcell" forIndexPath:indexPath];
    self.array = self.subArray[indexPath.section];
    cell.model = self.array[indexPath.item];
    NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=0", cell.model.id];
    [self.delegate getGiftMore:url];
    NSLog(@"%@",url);
}
@end
